import { localCache } from '@/utils/cache'
import { BASE_URL, TIME_OUT } from './config'
import HYRequest from './request'
import { LOGIN_TOKEN } from '@/global/constants'
// 根据类实例化一个对象 在模块中导入发送网络请求
const hyRequest = new HYRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestSuccessFn: (config) => {
      console.log('hyRequest请求成功的拦截')
      // 添加token
      // config.headers = {
      //   Authorization: 'Bearer' + localCache.getCache('token')
      // }
      const token = localCache.getCache(LOGIN_TOKEN)
      if (config.headers && token) {
        //类型缩小   Bearer要加个空格
        config.headers.Authorization = 'Bearer ' + token
      }
      return config
    }
  }
})
export const hyRequest2 = new HYRequest({
  baseURL: 'http://codercba.com:1888/airbnb/api',
  timeout: 8000,
  // new实例的时候 拦截器  添加额外的

  interceptors: {
    requestSuccessFn: (config) => {
      console.log('爱彼应的请求成功的拦截')
      return config
    },
    requestFailureFn: (err) => {
      console.log('爱彼应的请求失败的拦截')

      return err
    },
    responseSuccessFn: (res) => {
      console.log('爱彼应的响应成功的拦截')

      return res
    },
    responseFailureFn: (err) => {
      console.log('爱彼应的请求失败的拦截')

      return err
    }
  }
})
export default hyRequest
